package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.utils.UserContext;
import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.domain.NewsCommend;
import cn.wolfcode.trip.base.domain.Shop;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CommodityQueryObject;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.ShopQueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.ICommodityService;
import cn.wolfcode.trip.base.service.INewsCommendService;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.IShopService;
import cn.wolfcode.trip.base.utils.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @Autowired
    private ICommodityService commodityService;


    //首页显示商品
    @GetMapping("/commodity")
    @ResponseBody
    public Object getCommends(@ModelAttribute("qo") CommodityQueryObject qo){
        return commodityService.selectCommodity(qo);
    }

    @GetMapping
    @ResponseBody
    public Object shopcar(@ModelAttribute("qo") ShopQueryObject qo){
        qo.setState(0);
        return shopService.query(qo);
    }
    //购买商品
    @PutMapping("/{id}/commodity")
    @ResponseBody
    public Object update(@PathVariable Long id){
        JSONResult result = new JSONResult();

        try {
            Commodity commodity = commodityService.getmoneyByid(id);
            Integer money = commodity.getIntegral();
            Long userid = UserContext.getCurrentUser().getId();
            shopService.buyProduct(money,userid);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }
    //回显积分
    @GetMapping("/profiles")
    @ResponseBody
    public Object getmoney(){

        Long userid = UserContext.getCurrentUser().getId();

        return shopService.getMoneyByuserid(userid);
    }

    @GetMapping("/car")
    @ResponseBody
    public Object car(@ModelAttribute("qo") CommodityQueryObject qo) {
        qo.setState(0);
        return commodityService.query(qo);
    }

    //购物车购买商品
    @PutMapping("/{totalPrice}/buycommodity/{ids}")
    @ResponseBody
    public Object buy(@PathVariable int totalPrice,@PathVariable Long[] ids){
        JSONResult result = new JSONResult();

        try {
            Long userid = UserContext.getCurrentUser().getId();
            shopService.carbuyProduct(totalPrice,userid,ids);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }
    //购物车删除商品
    @DeleteMapping("/{id}/buycommodity")
    @ResponseBody
    public Object delete(@PathVariable Long id){
        JSONResult result = new JSONResult();

        try {
           commodityService.nostateByid(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }

}
